/*
 * This class is used to store data of history page with given time interval. Here time interval is two hrs.
 * all data will be store in the array list.
 */
package com.fipl.parse;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class DayParseVariable {

	private String urlStr = "http://megan.teksar.com.mx/jsonaverage.php?dbname=";
	private String urlStr2 = "&action=select&table=log&date=";			
	private String urlStr3 = "&limit=8&group_by= LIMIT 0,50";
	private StringBuffer jsonText = null;								// to store data from the url which is used to parse json.
	public ParserVariable parserVariable;								// Object of ParseerVariable class
	public ArrayList<String> ID = new ArrayList<String>();				// Store id in arraylist
	private HttpURLConnection httpURLConnection;						// to create http connection
	private BufferedReader bufferedReader;								//
	/*
	 *  integer value return 0 if no error found in retrieving 		
	 *  data and parsing json otherwise -1
	 */
	int parsedReturn = 0;												
	
	// constructor with database name, date, and time interval.
	public DayParseVariable(String dbName,String date,String interval) {
		// TODO Auto-generated constructor stub
		try{
			// concatinating and url with given parameters... also replace space with %20
			String newUrl = (urlStr+dbName+urlStr2+date+"&hours="+interval+urlStr3).replaceAll(" ", "%20");
			parserVariable = new ParserVariable();
			URL url = new URL(newUrl);													// create and initialise url object.
			httpURLConnection = (HttpURLConnection)url.openConnection();				// open http connection with gievn url.
			InputStream inputStream = httpURLConnection.getInputStream();				// receive input stream from http connection.
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);	// read the input stream
			bufferedReader= new BufferedReader(inputStreamReader);						// input stream stored in buffered reader
			String lineBuffer = null ;													// will store line by line reading from buffered reader
			jsonText = new StringBuffer();												// initialise string buffer
			/*
			 * read line by line data from the url and store in string buffered jsonText.
			 */
			do{
				lineBuffer = bufferedReader.readLine();
				Log.d("System out", ""+lineBuffer);
				jsonText.append(lineBuffer);
			}while(lineBuffer!=null);
			httpURLConnection.disconnect();												// disconnect http connection
			bufferedReader.close();														// close buffered reader	
			
		}catch (Exception e) {
			// TODO: handle exception
			parsedReturn=-1;															// return -1 if any exception is thrown
		}
	}

	public int parseData(){

		if(!(parsedReturn==-1)){
			JSONArray jsonArray;
			try {
				jsonArray= new JSONArray(jsonText.toString());
				for(int i=0; i<jsonArray.length();i++){
					JSONObject jsonObject = jsonArray.getJSONObject(i);

					//ID.add(jsonObject.getString("ID"));	
					parserVariable.iDArrayList.add(jsonObject.getString("ID"));
					parserVariable.fechaArrayList.add(jsonObject.getString("fecha"));
					//parserVariable.setEquipoString(jsonObject.getString("equipo"));

					//get temperature data from the json file and store it in array list
					parserVariable.Temperature_1_ArrayList.add(Double.parseDouble(jsonObject.getString("Temperature_1")));
					parserVariable.Temperature_2_ArrayList.add(Double.parseDouble(jsonObject.getString("Temperature_2")));
					parserVariable.Temperature_3_ArrayList.add(Double.parseDouble(jsonObject.getString("Temperature_3")));
					parserVariable.Temperature_4_ArrayList.add(Double.parseDouble(jsonObject.getString("Temperature_4")));

					// get humidity data from the json file and store it in array list
					parserVariable.Humidity_1_ArrayList.add(Double.parseDouble(jsonObject.getString("Humidity_1")));
					parserVariable.Humidity_2_ArrayList.add(Double.parseDouble(jsonObject.getString("Humidity_2")));
					parserVariable.Humidity_3_ArrayList.add(Double.parseDouble(jsonObject.getString("Humidity_3")));
					parserVariable.Humidity_4_ArrayList.add(Double.parseDouble(jsonObject.getString("Humidity_4")));
					
					// get voltage data from the json file and store it in array list
					parserVariable.Voltage_1A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_1A")));
					parserVariable.Voltage_1B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_1B")));
					parserVariable.Voltage_1C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_1C")));
					parserVariable.Voltage_2A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_2A")));
					parserVariable.Voltage_2B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_2B")));
					parserVariable.Voltage_2C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_2C")));
					parserVariable.Voltage_3A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_3A")));
					parserVariable.Voltage_3B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_3B")));
					parserVariable.Voltage_3C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_3C")));
					parserVariable.Voltage_4A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_4A")));
					parserVariable.Voltage_4B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_4B")));
					parserVariable.Voltage_4C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_4C")));
					parserVariable.Voltage_5A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_5A")));
					parserVariable.Voltage_5B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_5B")));
					parserVariable.Voltage_5C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_5C")));
					parserVariable.Voltage_6A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_6A")));
					parserVariable.Voltage_6B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_6B")));
					parserVariable.Voltage_6C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_6C")));
					parserVariable.Voltage_7A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_7A")));
					parserVariable.Voltage_7B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_7B")));
					parserVariable.Voltage_7C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_7C")));
					parserVariable.Voltage_8A_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_8A")));
					parserVariable.Voltage_8B_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_8B")));
					parserVariable.Voltage_8C_ArrayList.add(Double.parseDouble(jsonObject.getString("Voltage_8C")));				

					// // get current data from the json file and store it in array list
					parserVariable.Current_1A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_1A")));
					parserVariable.Current_1B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_1B")));
					parserVariable.Current_1C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_1C")));
					parserVariable.Current_2A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_2A")));
					parserVariable.Current_2B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_2B")));
					parserVariable.Current_2C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_2C")));
					parserVariable.Current_3A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_3A")));
					parserVariable.Current_3B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_3B")));
					parserVariable.Current_3C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_3C")));
					parserVariable.Current_4A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_4A")));
					parserVariable.Current_4B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_4B")));
					parserVariable.Current_4C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_4C")));
					parserVariable.Current_5A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_5A")));
					parserVariable.Current_5B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_5B")));
					parserVariable.Current_5C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_5C")));
					parserVariable.Current_6A_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_6A")));
					parserVariable.Current_6B_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_6B")));
					parserVariable.Current_6C_ArrayList.add(Double.parseDouble(jsonObject.getString("Current_6C")));
					
					// get PSTemperature data from the json file and store it in array list
					parserVariable.PSTemperature_1_ArrayList.add(Double.parseDouble(jsonObject.getString("PSTemperature_1")));
					parserVariable.PSTemperature_2_ArrayList.add(Double.parseDouble(jsonObject.getString("PSTemperature_2")));

					// get Diesel data from the json file and store it in array list
					parserVariable.Diesel_1_ArrayList.add(Double.parseDouble(jsonObject.getString("Diesel_1")));
					parserVariable.Diesel_2_ArrayList.add(Double.parseDouble(jsonObject.getString("Diesel_2")));

					// get DigitalInput data from the json file and store it in array list
					parserVariable.DigitalInput_1_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_1")));
					parserVariable.DigitalInput_2_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_2")));
					parserVariable.DigitalInput_3_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_3")));
					parserVariable.DigitalInput_4_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_4")));
					parserVariable.DigitalInput_5_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_5")));
					parserVariable.DigitalInput_6_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_6")));
					parserVariable.DigitalInput_7_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_7")));
					parserVariable.DigitalInput_8_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_8")));
					parserVariable.DigitalInput_9_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_9")));
					parserVariable.DigitalInput_10_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_10")));
					parserVariable.DigitalInput_11_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_11")));
					parserVariable.DigitalInput_12_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_12")));
					parserVariable.DigitalInput_13_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_13")));
					parserVariable.DigitalInput_14_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_14")));
					parserVariable.DigitalInput_15_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_15")));
					parserVariable.DigitalInput_16_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_16")));
					parserVariable.DigitalInput_17_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_17")));
					parserVariable.DigitalInput_18_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_18")));
					parserVariable.DigitalInput_19_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_19")));
					parserVariable.DigitalInput_20_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_20")));
					parserVariable.DigitalInput_21_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_21")));
					parserVariable.DigitalInput_22_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_22")));
					parserVariable.DigitalInput_23_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_23")));
					parserVariable.DigitalInput_24_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalInput_24")));

					// get DigitalOutput data from the json file and store it in array list
					parserVariable.DigitalOutput_1_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_1")));
					parserVariable.DigitalOutput_2_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_2")));
					parserVariable.DigitalOutput_3_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_3")));
					parserVariable.DigitalOutput_4_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_4")));
					parserVariable.DigitalOutput_5_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_5")));
					parserVariable.DigitalOutput_6_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_6")));
					parserVariable.DigitalOutput_7_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_7")));
					parserVariable.DigitalOutput_8_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_8")));
					parserVariable.DigitalOutput_9_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_9")));
					parserVariable.DigitalOutput_10_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_10")));
					parserVariable.DigitalOutput_11_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_11")));
					parserVariable.DigitalOutput_12_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_12")));
					parserVariable.DigitalOutput_13_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_13")));
					parserVariable.DigitalOutput_14_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_14")));
					parserVariable.DigitalOutput_15_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_15")));
					parserVariable.DigitalOutput_16_ArrayList.add(Double.parseDouble(jsonObject.getString("DigitalOutput_16")));

				}
				//Log.d("System out", "ID is "+ID.toString());
				//variable.setiDArrayList(ID);
				/*
				 * this will mapped data
				 */
				parserVariable.setMapping();

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				parsedReturn = -1;								// return -1 if any exception is thrown.
			}	
		}

		return parsedReturn;
	}

}
